package data_structure.mooc;

import data_structure.implement.StackImp;

/**
 * Based on the "Array" defined myself
 * an implement of StackImp
 * @param <E>
 */
public class ArrayStack<E> implements StackImp<E> {

    private Array<E> stack;

    public ArrayStack(int capacity){
        stack=new Array<> (capacity);
    }
    public ArrayStack(){
        stack=new Array<>();
    }

    @Override
    public void push(E e) {
        stack.addLast (e);
    }

    @Override
    public E pop() {
        return stack.removeLast ();
    }

    @Override
    public E peek() {
        return stack.getLast ();
    }

    @Override
    public boolean isEmpty() {
        return stack.isEmpty();
    }

    @Override
    public int getSize() {
        return stack.getSize ();
    }

    public int getCapacity(){
        return stack.getCapacity();
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder("stack:");
        sb.append ("[");
        for(int i=0;i<stack.getSize ();i++){
            sb.append (stack.get (i));
            if(i!=stack.getSize () - 1){
                sb.append (",");
            }
        }
        sb.append ("] top");

        return sb.toString ();
    }

    public static void main(String[] args) {
        ArrayStack<Integer>stack = new ArrayStack<> ();

        for(int i=0;i<stack.getCapacity();i++){
            stack.push (i);
            System.out.println(stack.toString ());
        }
        stack.pop ();
        System.out.println(stack.toString());
        stack.push (100);
        System.out.println(stack.toString());
    }
}
